package com.czmh.etc.onlinepubsys.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czmh.etc.onlinepubsys.dto.SysConfigParam;
import com.czmh.etc.onlinepubsys.maper.SysConfigMapper;
import com.czmh.etc.onlinepubsys.model.SysConfig;
import com.czmh.etc.onlinepubsys.services.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 系统参数配置 服务实现类
 * </p>
 *
 * @author eric
 * @since 2021-07-05
 */
@Service
public class SysConfigServiceImpl implements SysConfigService {
    @Autowired
    SysConfigMapper sysConfigMapper;

    @Override
    public IPage<SysConfig> queryList(SysConfigParam queryParam, Integer pageSize, Integer pageNum) {
        IPage<SysConfig> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysConfig> wrapper = new QueryWrapper();
        LambdaQueryWrapper<SysConfig> lambda = wrapper.lambda();
        lambda.like(SysConfig::getConfigKey, queryParam.getConfigKey());
        lambda.like(SysConfig::getConfigName, queryParam.getConfigName());
        lambda.like(SysConfig::getConfigOptions, queryParam.getConfigOptions());
        lambda.eq(SysConfig::getConfigurable, queryParam.getConfigurable());
        lambda.like(SysConfig::getConfigValue, queryParam.getConfigValue());
        lambda.eq(SysConfig::getDataType, queryParam.getDataType());
        lambda.in(SysConfig::getId, queryParam.getIds());
        lambda.eq(SysConfig::getId, queryParam.getId());
        return sysConfigMapper.selectPage(page, wrapper);
    }

    @Override
    public SysConfig queryByKey(String key) {
        QueryWrapper<SysConfig> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<SysConfig> lambda = wrapper.lambda();
        lambda.eq(SysConfig::getConfigKey, key);
        List<SysConfig> sysConfigs = sysConfigMapper.selectList(lambda);
        if (CollectionUtils.isEmpty(sysConfigs)) {
            return null;
        } else {
            return sysConfigs.get(0);
        }
    }
}
